<template>
  <Dialog v-model="dialogVisible" title="转移客户">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-form-item label="转移客户" prop="customerIds">
        <el-select
          multiple
          collapse-tags
          collapse-tags-tooltip
          :max-collapse-tags="3"
          value-key="id"
          v-model="formData.customerIds"
          placeholder="请选择客户"
        >
          <el-option
            v-for="item in customerList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="目标员工" prop="ownerUserId" required>
        <el-select v-model="formData.ownerUserId" placeholder="请选择员工">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">立刻指派</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { CustomerApi, CustomerVO } from '@/api/crm/crmcustomer'
import * as UserApi from '@/api/system/user'
defineOptions({ name: 'TransferTemplateForm' })
const emit = defineEmits(['success']) // 定义可触发的事件
const message = useMessage() // 消息弹窗
const customerList = ref([])
const userList = ref([])
const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  customerIds: undefined,
  ownerUserId: undefined,
  isCustomer: true
})
const formRules = reactive({
  ownerUserId: [{ required: true, message: '目标员工不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (customers, type) => {
  dialogVisible.value = true
  resetForm()
  // 设置数据
  formData.value.isCustomer = type
  formLoading.value = true

  let idsArr = []
  customers.forEach((value) => {
    idsArr.push(value.id)
  })
  customerList.value = customers
  formData.value.customerIds = idsArr

  try {
    userList.value = await UserApi.getUserListAllSimple({})
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = {
      customerIds: formData.value.customerIds,
      ownerUserId: formData.value.ownerUserId
    }

    await CustomerApi.assignCustomer(data)

    message.success('指派成功')
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    customerIds: undefined,
    ownerUserId: undefined
  }
  formRef.value?.resetFields()
}
</script>
